package com.ztf0337.mycurriculumdesign.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.Utils.CompareClassUtil;
import com.ztf0337.mycurriculumdesign.Utils.TimeConverters;
import com.ztf0337.mycurriculumdesign.entity.ParkingSpace;
import com.ztf0337.mycurriculumdesign.entity.RentedParkingSpace;

import java.util.List;

public class RentedParkingSpaceAdapter  extends RecyclerView.Adapter<RentedParkingSpaceAdapter.ViewHolder>{
    private List<RentedParkingSpace> rentedParkingSpaces;
    private OnItemClickListener listener;  // 监听器引用

    public RentedParkingSpaceAdapter(List<RentedParkingSpace> rentedParkingSpaces) {
        this.rentedParkingSpaces = rentedParkingSpaces;
    }

    // 定义点击事件接口
    public interface OnItemClickListener {
        void onItemClick(RentedParkingSpace rentedParkingSpace);
    }

    // 提供设置监听器的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RentedParkingSpaceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_rented_parking_space, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RentedParkingSpaceAdapter.ViewHolder holder, int position) {
        RentedParkingSpace rentedParkingSpace = rentedParkingSpaces.get(position);
        holder.parkingInfoTextView.setText(rentedParkingSpace.getParkingInfo());
        holder.localTextView.setText(rentedParkingSpace.getLocation());
        holder.tvTimeKeeping.setText(TimeConverters.calculateHours(rentedParkingSpace.getStartTime(),TimeConverters.getNewTime())+" 小时");
        // 给每个item绑定点击事件，调用回调接口
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(rentedParkingSpace);
            }
        });
    }

    @Override
    public int getItemCount() {
        return rentedParkingSpaces.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView parkingInfoTextView,localTextView,tvTimeKeeping;
        public ViewHolder(View view) {
            super(view);
            parkingInfoTextView = view.findViewById(R.id.tvParkingInfo);
            localTextView = view.findViewById(R.id.tvLocal);
            tvTimeKeeping = view.findViewById(R.id.tvTimeKeeping);
        }
    }

    // 更新整个数据集
    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<RentedParkingSpace> newData) {
        this.rentedParkingSpaces = newData;
        notifyDataSetChanged();  // 简单刷新全部（低效）
    }

    // 只新增一项
    public void addItem(RentedParkingSpace newItem) {
        rentedParkingSpaces.add(newItem);
        notifyItemInserted(rentedParkingSpaces.size() - 1);
    }

    // 修改一项
    public void updateItem(RentedParkingSpace newItem) {
        rentedParkingSpaces.set(CompareClassUtil.compareRentedParkingSpace(rentedParkingSpaces,newItem), newItem);
        notifyItemChanged(CompareClassUtil.compareRentedParkingSpace(rentedParkingSpaces,newItem));
    }

    // 删除一项
    public void removeItem(RentedParkingSpace newItem) {
        int index = CompareClassUtil.compareRentedParkingSpace(rentedParkingSpaces, newItem);
        if (index != -1) {
            rentedParkingSpaces.remove(index);
            notifyItemRemoved(index);
        }
    }
}
